﻿package cn.poco.pepsi.activity.search;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.provider.Settings;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import cn.poco.pepsi.R;
import cn.poco.pepsi.TabTest;
import cn.poco.pepsi.activity.act.Find_atc_Activity;
import cn.poco.pepsi.activity.discut.DiscutMoreActivity;
import cn.poco.pepsi.adapter.SearchMainListAdapt;
import cn.poco.pepsi.bean.Circle;
import cn.poco.pepsi.bean.Dish;
import cn.poco.pepsi.bean.HotSearchKey;
import cn.poco.pepsi.service.ISearch;
import cn.poco.pepsi.service.impl.SearchImpl;
import cn.poco.pepsi.util.Cons;

/**
 * 搜索主页
 * 
 * @author tanglx
 * 
 */

public class SearchMainActivity extends Activity {
	public static final String TAG = "SearchMainActivity";
	public static final String CATA = "dish";
	// 官方城市代码
	public static final String[] OfficialCityCode = { "101029001", "101022001",
			"101001001", "101003001" };
	private ListView lView1, lView2, lView3, lView4;// 三个加载数据的列表 lView4热门搜索
	private LinearLayout additionLayout, progressLayout, errorLayout,
			search_main_id;
	private Handler tHandler;
	private String cityCode, cityName;
	private List<Circle> list;
	private List<Dish> list2;
	private List<String> list3;
	private List<HotSearchKey> list5;// 热门搜索
	private List<String> list4;// 测试
	private SearchMainListAdapt adapter, adapter1, adapter2, adapter4;
	private ScrollView scrollView;
	private Boolean isOfficialCity = false;
	private EditText searchText;
	private Button tautology;
	private Button setBtn;
	private TextView mainTitil;

	private void InitView() {

		mainTitil = (TextView) findViewById(R.id.show_title_search_main);

		lView1 = (ListView) findViewById(R.id.listview);// 热门商区
		lView2 = (ListView) findViewById(R.id.search_main_list2);// 热门菜系
		lView3 = (ListView) findViewById(R.id.search_main_list3);// 其他
		lView4 = (ListView) findViewById(R.id.search_main_list4);// 热门搜索
		
		lView1.setDivider(null);
		lView1.setDividerHeight(0);
		lView2.setDivider(null);
		lView2.setDividerHeight(0);
		lView3.setDivider(null);
		lView3.setDividerHeight(0);
		lView4.setDivider(null);
		lView4.setDividerHeight(0);

		progressLayout = (LinearLayout) findViewById(R.id.progressbar);
		additionLayout = (LinearLayout) findViewById(R.id.search_main_addition);
		scrollView = (ScrollView) findViewById(R.id.search_main_scrollview);
		searchText = (EditText) findViewById(R.id.searchtext);

		errorLayout = (LinearLayout) findViewById(R.id.error);
		search_main_id = (LinearLayout) findViewById(R.id.search_main_id);

		tautology = (Button) findViewById(R.id.tautology);
		setBtn = (Button) findViewById(R.id.setnet);

		cityCode = getIntent().getExtras().getString(Cons.CITY_CODE);
		cityName = getIntent().getExtras().getString(Cons.CITY_NAME);

		mainTitil.setText("百事美食[" + cityName + "]");
		// 是否是官方城市区域
		for (int i = 0; i < OfficialCityCode.length; i++) {
			if (cityCode.equals(OfficialCityCode[i])) {
				isOfficialCity = true;
			}
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search_main);

		InitView();

		HandlerThread ht = new HandlerThread(TAG);
		ht.start();
		tHandler = new Handler(ht.getLooper());
		tHandler.post(runnable);

		searchText.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onSearchRequested();
			}
		});

		lView1.setOnItemClickListener(new OnItemClickListener() {
			// 热门商区
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (position == 4) {
					// 更多
					Intent intent = new Intent(SearchMainActivity.this,
							SearchMainMoreAreaActivity.class);
					Bundle bundle = new Bundle();
					bundle.putString(Cons.CITY_CODE, cityCode);
					bundle.putString(Cons.CITY_NAME, cityName);
					intent.putExtras(bundle);
					startActivity(intent);
				} else {
					// 具体商区
					Intent intent = new Intent(SearchMainActivity.this,
							SearchResultActivity.class);
					intent.putExtra(Cons.CITY_NAME, cityName);
					intent.putExtra(Cons.ACTIVITY_TOP_TITLE, list.get(position)
							.getName());
					intent.putExtra(Cons.CIRCLE_CODE, list.get(position)
							.getKey());
					intent.putExtra(Cons.ISPEPSI_RES, "no");
					startActivity(intent);
				}
			}

		});

		lView2.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (position == 4) {
					// 热门菜系 更多
					Intent intent = new Intent(SearchMainActivity.this,
							SearchMainMoreDishActivity.class);
					intent.putExtra(Cons.CITY_CODE, cityCode);
					intent.putExtra(Cons.CITY_NAME, cityName);
					startActivity(intent);
				} else {
					Intent intent = new Intent(SearchMainActivity.this,
							SearchResultActivity.class);
					intent.putExtra(Cons.ACTIVITY_TOP_TITLE, list2
							.get(position).getName());
					intent.putExtra(Cons.CITY_NAME, cityName);
					intent.putExtra(Cons.SEARCH_DISHID, list2.get(position)
							.getId());
					intent.putExtra(Cons.ISPEPSI_RES, "no");
					startActivity(intent);
				}

			}
		});

		lView4.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (position == 4) {
					// 热门搜索 更多
					Intent intent = new Intent(SearchMainActivity.this,
							MainMoreHotKeyActivity.class);
					startActivity(intent);
				} else {
					Intent intent = new Intent(SearchMainActivity.this,
							SearchResultActivity.class);
					intent.putExtra(Cons.HOT_SEARCH_KEYWORD, list5
							.get(position).getKeyword());
					intent.putExtra(Cons.CITY_NAME, cityName);
					intent.putExtra(Cons.ISPEPSI_RES, "no");
					startActivity(intent);
				}

			}
		});

		lView3.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// position 从零开始
				if (position == 0) {
					Intent intent = new Intent(SearchMainActivity.this,
							SearchResultActivity.class);
					intent.putExtra(Cons.ISPEPSI_RES, "#ispepsi_poco_xw#");
					startActivity(intent);
				} 
				
				if (position == 1) {
						Intent intent = new Intent(SearchMainActivity.this,
								DiscutMoreActivity.class);
						startActivity(intent);
					} 
					
				if(position == 2){
					
					Intent intent = new Intent(SearchMainActivity.this,
							Find_atc_Activity.class);
					startActivity(intent);
				}
					

				
			}
		});

		setBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
				startActivity(intent);
			}
		});

		tautology.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				tHandler.post(runnable);
			}
		});
	}

	@Override
	public boolean onSearchRequested() {
		Bundle bundle = new Bundle();
		bundle.putString(Cons.CITY_CODE, cityCode);
		startSearch("", false, null, false);
		return true;
	}

	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			// 分别更新三个listview
			case 1:
				LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
						LayoutParams.FILL_PARENT, 300);
				lp.leftMargin = 15;
				lp.rightMargin = 15;

				adapter = new SearchMainListAdapt(SearchMainActivity.this,
						list, 1);
				lView1.setVisibility(View.VISIBLE);
				lView1.setAdapter(adapter);
				setListViewHeightBasedOnChildren(lView1);

				System.out.println("list2.size()" + list2.size());
				adapter1 = new SearchMainListAdapt(SearchMainActivity.this,
						list2, 2);
				lView2.setVisibility(View.VISIBLE);
				lView2.setAdapter(adapter1);
				setListViewHeightBasedOnChildren(lView2);

				System.out.println("list5.size()" + list5.size());
				adapter4 = new SearchMainListAdapt(SearchMainActivity.this,
						list5, 4);
				lView4.setVisibility(View.VISIBLE);
				lView4.setAdapter(adapter4);
				setListViewHeightBasedOnChildren(lView4);

				// 是否是官方城市
				if (isOfficialCity) {
					additionLayout.setVisibility(View.VISIBLE);
					adapter2 = new SearchMainListAdapt(SearchMainActivity.this,
							list3, 3);
					lView3.setAdapter(adapter2);
					lView3.setVisibility(View.VISIBLE);
					setListViewHeightBasedOnChildren(lView3);
				}

				scrollView.setVisibility(View.VISIBLE);
				progressLayout.setVisibility(View.GONE);

				break;

			case Cons.CONN_ERROR:
				search_main_id.setVisibility(View.GONE);
				progressLayout.setVisibility(View.GONE);
				Toast.makeText(SearchMainActivity.this, "获取城市数据出错",
						Toast.LENGTH_SHORT).show();
				break;
			default:
				break;
			}
		};
	};

	/**
	 * 获取热门商区 和 热门菜系
	 */
	Runnable runnable = new Runnable() {
		@Override
		public void run() {
			try {
				ISearch search = new SearchImpl(SearchMainActivity.this);

				// 热门商区
				list = search.getHotCircle(cityCode, new Integer(1));
				search.getArea(cityCode, SearchMainActivity.this);
				Circle circle = new Circle();
				circle.setKey("0000");
				circle.setName("更多");

				Dish dish = new Dish();
				dish.setCitycode(cityCode);
				dish.setId(0000l);
				dish.setName("更多");

				HotSearchKey hotKey = new HotSearchKey();
				hotKey.setKeyword("更多");

				if (list.size() > 5) {
					list.add(4, circle);
				} else {
					list.add(circle);
				}

				// 热门菜系
				list2 = search
						.getDishs(cityCode, CATA, SearchMainActivity.this);
				if (list2.size() > 5) {
					list2.add(4, dish);
				} else {
					list2.add(dish);
				}

				// 热门搜索
				list5 = search.getHotkey();

				if (list5.size() > 5) {
					list5.add(4, hotKey);
				} else {
					list5.add(hotKey);
				}

				if (isOfficialCity) {
					list3 = new ArrayList<String>();
					list3.add("百事加盟餐厅");
					list3.add("热门优惠");
					list3.add("觅食专题");
					list3.add("美食活动");
				}

				handler.sendEmptyMessage(1);
			} catch (Exception e) {
				handler.sendEmptyMessage(Cons.CONN_ERROR);
				e.printStackTrace();
			}
		}
	};

	/**
	 * 传入一个listView，并设置这个listview高度
	 * 
	 * @param listView
	 */
	public void setListViewHeightBasedOnChildren(ListView listView) {
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			return;
		}

		// 算出两个View间距
		int totalHeight = 0;
		for (int i = 0; i < listAdapter.getCount(); i++) {
			View listItem = listAdapter.getView(i, null, listView);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}

		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = totalHeight
				+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
		listView.setLayoutParams(params);
	}

}
